﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace UpDryTwistImageReader {

    class MatchingImages : List<string> {

        #region Constructor

        public MatchingImages(Configuration config) {

            m_configuration = config;
        }

        #endregion

        #region Private Methods

        private void loadDirectories() {

            if (DateTime.Now.Subtract(m_lastDirectoryLoad).Minutes >= m_configuration.reloadMinutes ) {
                // If our timeout has expired, force a directory reload ...
                m_directories = null;
            }

            if (m_directories == null) {
                // If first time or expired, reload.
                m_directories = new Directories();
                m_lastDirectoryLoad = DateTime.Now;
            }

            foreach ( string directory in m_configuration.directories ) {
                m_directories.addFolder( m_configuration, directory, m_configuration.fileTypes );  // won't re-add if not already added ...
            }
        }

        public void refreshImages() {

            int added = 0;
            loadDirectories();
            foreach ( string directory in m_configuration.directories ) {

                ImageList list = m_directories[directory];
                foreach (ImageFileAttributes attributes in list) {
                    if (attributes.Orientation == m_configuration.orientation) {
                        this.Add(attributes.Path);
                        added++;
                    }
                }
                m_configuration.log("Refreshing \"" + directory + "\" for orientation " + m_configuration.orientation.ToString() + " among " + m_directories[directory].Count.ToString() + " files has added " + added.ToString() + " so far.");
            }
        }

        #endregion

        #region Private Members

        private Configuration m_configuration;
        static DateTime m_lastDirectoryLoad = DateTime.Now;
        static private Directories m_directories = null;

        #endregion
    }
}
